//
//  GSCommonFindPassSmsVerifyController.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonFindPassSmsVerifyController.h"

#import "GSCommonLoginVerificationCodeView.h"

#import "GSCommonLoginVerificationCodeButton.h"

#import "GSCommonFindPassResetController.h"

#import "NSString+GSCommonLogin.h"

#import "NSString+RegexCategory.h"

//reuqest
#import "BCPhoneCaptchaRequest.h"//请求发送手机验证码
#import "BCEmailCaptchaRequest.h"//请求发送邮箱验证码
#import "BCFindPassValidatePhoneCaptchaRequest.h"//手机验证码验证
#import "FindPassValidateEmailCaptchaRequest.h"//邮箱验证码验证


@interface GSCommonFindPassSmsVerifyController ()

@property (nonatomic, strong) GSCommonLoginVerificationCodeView *verifyCodeView;

/**
 *GSCommonLoginVerificationCodeButton
 */
@property (nonatomic, strong) GSCommonLoginVerificationCodeButton *codeButton;

@end

@implementation GSCommonFindPassSmsVerifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.largeTitleLabel.text = GSLoginLocalized(@"Find With Captcha");
    NSString *account = [GSCommonLoginDataManager sharedData].findPassModel.phoneNumber;
    //
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    
    NSString *subStr;
    if (type==GSCCommonLoginAccountTypePhone) {
        account = [NSString phoneNumberPrivacyFrom:account];
        subStr = GSLoginLocalizedFormatString(@"Please get the 6 digits captcha by phone-%@",account);//
    }else {
        subStr = GSLoginLocalizedFormatString(@"Please get the 6 digits captcha by email-%@",account);
    }
    //
    NSMutableAttributedString *attriText = [[NSMutableAttributedString alloc] initWithString:subStr];
    NSRange range = [subStr rangeOfString:account];
    if (range.location!=NSNotFound) {
        [attriText addAttributes:@{NSForegroundColorAttributeName:GSLOGIN_THEME_COLOR} range:range];
    }
    self.subTitleLabel.attributedText = attriText;
    [self setupSubviews];
    //
    __weak typeof(self) wself = self;
    self.verifyCodeView.verifyCodeComplete = ^(BOOL compelte) {
        if (wself.doneButton.enabled!=compelte) {
            wself.doneButton.enabled = compelte;
        }
    };
    
    //开始倒计时
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusWaiting;
    
    //唤起输入
    [self.verifyCodeView beginInputAction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupSubviews {
    self.verifyCodeView = [GSCommonLoginVerificationCodeView new];
    self.verifyCodeView.verifyCodeDigits = 6;
    [self.view addSubview:self.verifyCodeView];
    //
    CGSize size = [GSCommonLoginVerificationCodeView viewSizeWithVerifyCodeDigits:self.verifyCodeView.verifyCodeDigits];
    [self.verifyCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.subTitleLabel.mas_bottom).offset(30.0);
        make.left.mas_equalTo(self.view).offset(21.0);
        make.size.mas_equalTo(size);
    }];
    //
    self.codeButton = [GSCommonLoginVerificationCodeButton new];
    [self.view addSubview:self.codeButton];
    [self.codeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.verifyCodeView.mas_bottom).offset(16.0);
        make.left.mas_equalTo(self.verifyCodeView);
        
    }];
    [self.codeButton addTarget:self action:@selector(codeButtonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    //
    [self.view addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeButton.mas_bottom).offset(76.0/2);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo([GSCommonLoginButton defaultSize]);
    }];
    self.doneButton.enabled = false;
    //
    [self.view layoutIfNeeded];
}

#pragma mark - 发送验证码
- (void)codeButtonDidClickedAction:(QMUIButton *)button {
    [self.view endEditing:true];
    NSString *account = [GSCommonLoginDataManager sharedData].findPassModel.phoneNumber;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    if (type==GSCCommonLoginAccountTypePhone) {
        [self requestPhoneCaptcha:account];
    }else {
        [self requestEmailCaptchaAddress:account];
    }
}
//发送手机验证码
- (void)requestPhoneCaptcha:(NSString *)phone {
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusSending;
    BCPhoneCaptchaRequest *requst = [BCPhoneCaptchaRequest new];
    requst.phone = phone;
    //
    WEAK_SELF
    [requst didRequestCompleted:^{
        
    }];
    [requst didRequestSuccessed:^(id result, id page) {
        wself.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusWaiting;
        BCPhoneCaptchaResponse *resp = result;
        if (resp.msg&&wself.view) {
            [QMUITips showInfo:resp.msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:resp.msg]];
        }
    }];
    [requst didRequestFailed:^(NSError *error) {
        wself.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusReSend;
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [requst requestServer];
}
//发送邮箱验证码
- (void)requestEmailCaptchaAddress:(NSString *)email {
    self.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusSending;
    BCEmailCaptchaRequest *request = [BCEmailCaptchaRequest new];
    request.email = email;
    WEAK_SELF
    [request didRequestCompleted:^{
        
    }];
    [request didRequestSuccessed:^(id result, id page) {
        wself.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusWaiting;
        //
        BCEmailCaptchaResponse *resp = result;
        if (resp.msg&&wself.view) {
            [QMUITips showInfo:resp.msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:resp.msg]];
        }
    }];
    [request didRequestFailed:^(NSError *error) {
        wself.codeButton.verifyCodeStatus = GSCommonLoginVerificationCodeStatusReSend;
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}

#pragma mark - 
- (void)doneButtonDidClickedAction {
    [self.view endEditing:true];
    [self requestCheckCaptcha:self.verifyCodeView.verifyCode];
}
- (void)requestCheckCaptcha:(NSString *)captcha {
    NSString *account = [GSCommonLoginDataManager sharedData].findPassModel.phoneNumber;
    GSCCommonLoginAccountType type = [NSString gscLoginAccountTypeWithString:account];
    if (type==GSCCommonLoginAccountTypePhone) {
        [self requestPhoneCaptchaCheck:captcha];
    }else {
        [self requestEmailCaptchaCheck:captcha];
    }
}
- (void)requestPhoneCaptchaCheck:(NSString *)captcha {
    [self.view showLoginToast];
    BCFindPassValidatePhoneCaptchaRequest *request = [BCFindPassValidatePhoneCaptchaRequest new];
    request.phone = [GSCommonLoginDataManager sharedData].findPassModel.phoneNumber;
    request.captcha = captcha;
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCFindPassValidatePhoneCaptchaResponse *resp = result;
        [wself userCheckCaptchaSuccessWithEncryption:resp.data.encryption];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}
- (void)requestEmailCaptchaCheck:(NSString *)captcha {
    [self.view showLoginToast];
    FindPassValidateEmailCaptchaRequest *request = [FindPassValidateEmailCaptchaRequest new];
    request.email = [GSCommonLoginDataManager sharedData].findPassModel.phoneNumber;
    request.captcha = captcha;
    
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view hideLoginToast];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        FindPassValidateEmailCaptchaResponse *resp = result;
        [wself userCheckCaptchaSuccessWithEncryption:resp.data.encryption];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *msg = error.domain;
            [QMUITips showError:msg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:msg]];
        }
    }];
    [request requestServer];
}
//
- (void)userCheckCaptchaSuccessWithEncryption:(NSString *)encryption {
    //更新数据
    [GSCommonLoginDataManager sharedData].findPassModel.verifyCode = self.verifyCodeView.verifyCode;
    [GSCommonLoginDataManager sharedData].findPassModel.encryption = encryption;
    //
    GSCommonFindPassResetController *findPassResetVC = [GSCommonFindPassResetController new];
    [self.navigationController pushViewController:findPassResetVC animated:true];
}

@end
